package com.eedi.framework.job.controller.org;


import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.job.controller.org.vo.job.OrgJobPageReq;
import com.eedi.framework.job.controller.org.vo.job.OrgJobResp;
import com.eedi.framework.job.controller.org.vo.job.OrgJobSaveReq;
import com.eedi.framework.job.dal.dataobject.OrgJobDO;
import com.eedi.framework.job.service.OrgJobService;
import com.eedi.framework.quartz.core.util.CronUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.context.annotation.DependsOn;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "经营界面 - 定时任务")
@RestController
@RequestMapping("/job")
@Validated
@DependsOn(value = {"schedulerManager"})
public class JobController {
    @Resource
    private OrgJobService orgJobService;

    @PostMapping("/create")
    @Operation(summary = "创建定时任务(org:job:create)")
    @PreAuthorize("@org.hasPermission('org:job:create')")
    public CommonResult<String> createJob(@Valid @RequestBody OrgJobSaveReq createReqVO)
            throws SchedulerException {
        return success(orgJobService.createJob(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定时任务(org:job:update)")
    @PreAuthorize("@org.hasPermission('org:job:update')")
    public CommonResult<Boolean> updateJob(@Valid @RequestBody OrgJobSaveReq updateReqVO)
            throws SchedulerException {
        orgJobService.updateJob(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新定时任务的状态(org:job:update)")
    @Parameters({
            @Parameter(name = "orgJobId", description = "编号", required = true, example = "1024"),
            @Parameter(name = "status", description = "状态", required = true, example = "1"),
    })
    @PreAuthorize("@org.hasPermission('org:job:update')")
    public CommonResult<Boolean> updateJobStatus(@RequestParam(value = "orgJobId") String orgJobId, @RequestParam("status") Integer status)
            throws SchedulerException {
        orgJobService.updateJobStatus(orgJobId, status);
        return success(true);
    }

	@DeleteMapping("/delete")
    @Operation(summary = "删除定时任务(org:job:delete)")
    @Parameter(name = "orgJobId", description = "编号", required = true, example = "1024")
	@PreAuthorize("@org.hasPermission('org:job:delete')")
    public CommonResult<Boolean> deleteJob(@RequestParam("orgJobId") String orgJobId)
            throws SchedulerException {
        orgJobService.deleteJob(orgJobId);
        return success(true);
    }

    @PutMapping("/trigger")
    @Operation(summary = "触发定时任务(org:job:update)")
    @Parameter(name = "orgJobId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:job:update')")
    public CommonResult<Boolean> triggerJob(@RequestParam("orgJobId") String orgJobId) throws SchedulerException {
        orgJobService.triggerJob(orgJobId);
        return success(true);
    }

    @PostMapping("/sync")
    @Operation(summary = "同步定时任务(org:job:update)")
    @PreAuthorize("@org.hasPermission('org:job:update')")
    public CommonResult<Boolean> syncJob() throws SchedulerException {
        orgJobService.syncJob();
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定时任务(org:job:update)")
    @Parameter(name = "orgJobId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:job:query')")
    public CommonResult<OrgJobResp> getJob(@RequestParam("orgJobId") String orgJobId) {
        OrgJobDO job = orgJobService.getJob(orgJobId);
        return success(BeanUtils.toBean(job, OrgJobResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得定时任务分页(org:job:query)")
    @PreAuthorize("@org.hasPermission('org:job:query')")
    public CommonResult<PageResult<OrgJobResp>> getJobPage(@Valid OrgJobPageReq pageVO) {
        PageResult<OrgJobDO> pageResult = orgJobService.getJobPage(pageVO);
        return success(BeanUtils.toBean(pageResult, OrgJobResp.class));
    }
    @GetMapping("/get_next_times")
    @Operation(summary = "获得定时任务的下 n 次执行时间(org:job:query)")
    @Parameters({
            @Parameter(name = "orgJobId", description = "编号", required = true, example = "1024"),
            @Parameter(name = "count", description = "数量", example = "5")
    })
    @PreAuthorize("@org.hasPermission('org:job:query')")
    public CommonResult<List<LocalDateTime>> getJobNextTimes(
            @RequestParam("orgJobId") String orgJobId,
            @RequestParam(value = "count", required = false, defaultValue = "5") Integer count) {
        OrgJobDO job = orgJobService.getJob(orgJobId);
        if (job == null) {
            return success(Collections.emptyList());
        }
        return success(CronUtils.getNextTimes(job.getCronExpression(), count));
    }

}
